/**
* 游戏特效汇总
* (c) copyright 2016 - 2035
* All Rights Reserved. 
* 使用方法如：EffectUtils.rotationEffect()
*/

module EffectUtils {
    //对象旋转特效
    //obj   旋转对象
    //time  旋转一周用时，毫秒
    export function rotationEffect(obj, time: number = 1000): void {
        egret.Tween.get(obj, { loop: true }).to({ rotation: 360 }, time).to({ rotation: 0 });
    }
    //抖动对象特效
    //类似ios密码输入错误的特效
    export function shakeObj(obj): void {
        var shakeNum = 80;
        var oldX: number = obj.x;
        egret.Tween.get(obj).to({ x: obj.x - 10 }, shakeNum);

        egret.setTimeout(function () {
            egret.Tween.get(obj).to({ x: obj.x + 20 }, shakeNum);
        }, this, shakeNum * 2);
        egret.setTimeout(function () {
            egret.Tween.get(obj).to({ x: obj.x - 20 }, shakeNum);
        }, this, shakeNum * 3);
        egret.setTimeout(function () {
            egret.Tween.get(obj).to({ x: obj.x + 20 }, shakeNum);
        }, this, shakeNum * 4);
        egret.setTimeout(function () {
            egret.Tween.get(obj).to({ x: oldX }, shakeNum);
        }, this, shakeNum * 5);
    }
    /**
    * 显示对象上线浮动特效
    * obj           对象
    * time          浮动时间 毫秒
    *delayed        延时执行
    * space         浮动高度
    */
    export function flyObj(obj, time = 300, delayed: number = 0, space: number = 50, call: Function = null): void {
        var onComplete1: Function = function () {
            if (obj != null) {
                var onComplete2: Function = function () {
                    egret.Tween.get(obj).to({ y: obj.y + space }, time).call(() => {
                        egret.Tween.removeTweens(obj);
                        if (call != null) {
                            call();
                        }
                    }, this);
                };
                egret.Tween.get(obj).wait(delayed).to({ y: obj.y - space }, time).call(onComplete2, this);
            }
        };
        onComplete1();
    }
    /**
    * 让显示对象变暗后再变亮
    * obj           对象
    * time          变暗维持时间
    */
    export function playDim(obj, time = 100): void {
        egret.Tween.get(obj).to({ alpha: 0.2 }, 100).wait(time).to({ alpha: 1 });
    }
    /**
    * 图片旋转后换图- 使用此动画的对象必须把锚点设置到中间
    * source 要换成什么图
    * time 时间
    * img 处理的对象
    */
    export function xuanZuanHuanTu(img: eui.Image, source: any, time: number = 300, call: Function = null): void {
        let scale_x = img.scaleX

        // let x = img.x
        // let anchorOffsetX = img.anchorOffsetX;
        // img.anchorOffsetX = img.width / 2;
        // img.x += img.width / 2;
        egret.Tween.get(img)
            .to({ scaleX: 0 }, time)
            .to({ source: source })
            .to({ scaleX: scale_x }, time).call(function () {
                // img.anchorOffsetX = anchorOffsetX;
                // img.x = x;
                if (call != null) {
                    call();
                }
            }
            );
    }
    /**
     * - 使用此动画的对象必须把锚点设置到中间
     * 拉伸动画 左右拉宽回缩 上下拉宽回缩 类似果冻掉在地上
     * 
     */
    export function Stretch(img: egret.DisplayObject, call: Function = null) {
        let scale_x = img.scaleX
        let scale_y = img.scaleY
        // let anchorOffsetX = img.anchorOffsetX;
        // let anchorOffsetY = img.anchorOffsetY;
        // img.anchorOffsetX = img.width / 2;
        // img.anchorOffsetY = img.height / 2;
        // img.x += (img.width * scale_x) / 2;
        // img.y += (img.height * scale_y) / 2;
        egret.Tween.get(img)
            .to({ scaleX: scale_x + 0.3, scaleY: scale_y - 0.3 }, 200, egret.Ease.circOut)
            .to({ scaleX: scale_x - 0.3, scaleY: scale_y + 0.3 }, 200, egret.Ease.circOut)
            .to({ scaleX: scale_x, scaleY: scale_y }, 200, egret.Ease.circOut).call(function () {
                // img.anchorOffsetX = anchorOffsetX;
                // img.anchorOffsetY = anchorOffsetY;
                // img.x -= (img.width * scale_x) / 2;
                // img.y -= (img.height * scale_y) / 2;
                if (call != null) {
                    call();
                }
            }
            );
    }
    //闪烁
    /*
    obj 闪烁对象
    index 闪烁次数 -1 循环播放 默认闪3下
    time 闪烁速度间隔
    */
    export function Twinkle(obj: egret.DisplayObject, index: number = 3, time: number = 200, call: Function = null) {
        try {
            obj.visible = true
            if (index == -1) {
                egret.Tween.get(obj, { loop: true }).to({ alpha: 0 }, time).to({ alpha: 1 }, time);;
            } else {
                let int = 0;
                egret.Tween.get(obj, { loop: true }).to({ alpha: 0 }, time).to({ alpha: 1 }, time).call(() => {
                    int++;
                    if (int >= index) {
                        egret.Tween.removeTweens(obj);
                        if (call != null) {
                            call();
                        }
                    }
                })
            }
        } catch (err) {
            egret.log(err);
        }
    }
    /**
     * 在当前panel层显示文字并渐变隐藏
     */
    export let textFadeAwayArray: Array<eui.Group> = [];
    export function textFadeAway(text: string) {
        var group = new eui.Group();
        var yuyin_text = new eui.Label;
        yuyin_text.text = text;
        yuyin_text.fontFamily = "Microsoft YaHei";
        yuyin_text.size = 20;
        // yuyin_text.bold = true;
        yuyin_text.textColor = 0xfffc00
        // yuyin_text.maxWidth = 800;

        var yuyin_bg = new eui.Image("tipsback");
        yuyin_bg.width = yuyin_text.width + 200;
        // yuyin_bg.height = yuyin_text.height + 36;
        yuyin_text.y = 12;
        yuyin_text.x = 100

        group.addChild(yuyin_bg);
        group.addChild(yuyin_text);
        group.horizontalCenter = "0";
        group.y = 0.4 * GameConfig.curHeight();
        group.touchEnabled = false
        group.touchChildren = false
        group.touchThrough = true;
        for (let i in textFadeAwayArray) {
            textFadeAwayArray[i].y -= textFadeAwayArray[i].height - 10;//10间距
        }
        GameLayerManager.gameLayer().effectLayer.addChild(group);
        textFadeAwayArray.push(group);

        var fun: Function = function (group: eui.Group) {
            egret.Tween.removeTweens(group)
            if (GameLayerManager.gameLayer().effectLayer.contains(group)) {
                GameLayerManager.gameLayer().effectLayer.removeChild(group);
            }
            for (let i = 0; i < textFadeAwayArray.length; i++) {
                if (textFadeAwayArray[i] == group) {
                    textFadeAwayArray.splice(i, 1);
                    break;
                }
            }
        }
        egret.Tween.get(group).to({ scaleX: 1.2, scaleY: 1.2 }, 200).to({ scaleX: 1, scaleY: 1 }, 200).wait(1000).to({ alpha: 0 }, 500).call(fun, this, [group]);
    }
    //显示等待界面
    export let WaitPanel: utils.WaitPanel
    export function showWaritPanel(): void {
        hideWaritPanel();
        WaitPanel = new utils.WaitPanel()
        GameLayerManager.gameLayer().maskLayer.addChild(WaitPanel);
    }
    //移除等待界面
    export function hideWaritPanel(): void {
        if (GameLayerManager.gameLayer().maskLayer.contains(WaitPanel)) {
            GameLayerManager.gameLayer().maskLayer.removeChild(WaitPanel);
        }
        WaitPanel = null;
    }


    //滚动消息队列
    export let noticeArr: Array<Array<any>> = []
    export let noticeArrNnterval//每秒轮询队列中的消息
    export function pushNotice(arr: Array<any>) {
        noticeArr.push(arr)
        if (noticeArrNnterval == null) {
            noticeArrNnterval = egret.setInterval(() => {
                if (noticeNnterval == null) {
                    showNotice(noticeArr[0]);
                    noticeArr.splice(0, 1);
                    if (noticeArr.length == 0) {
                        egret.clearInterval(noticeArrNnterval);
                        noticeArrNnterval = null;
                    }
                }
            }, this, 1000)
        }
    }
    //滚动消息单次播放
    export var noticeGroup: eui.Group;//装滚动公告组件的容器，大厅和各游戏界面中都有一个，切换到哪个界面，该界面就把那个容器传过来，这里把滚动公告移入那个容器
    export var noticeCom: components.noticeCom;//滚动公告组件，单例复用
    export var noticeRect: egret.Rectangle;//滚动公告视域窗口
    export var noticeNnterval;//滚动公告定时器，滚动完后移除公告组件并清除定时器赋值null，上面那个队列才会推送新的公告下来
    export function showNotice(arr: Array<any>) {
        if (Global.isEmpty(arr)) {
            return;
        }
        if (Global.isEmpty(noticeGroup)) {
            return;
        }
        if (noticeCom == null) {
            noticeCom = new components.noticeCom();
        }
        //新一轮公告要先根据当前所在面板的公告容器重置滚动公告组件的宽度和起始点
        noticeCom.textField.scrollRect.x = - (noticeGroup.width - 40)
        noticeCom.textField.scrollRect.width = noticeGroup.width - 50
        noticeCom.noticeBg.width = noticeGroup.width
        noticeCom.textField.textFlow = arr;
        noticeGroup.addChild(noticeCom);

        noticeNnterval = egret.setInterval(function () {
            noticeRect = noticeCom.textField.scrollRect;
            noticeRect.x += 2;
            noticeCom.textField.scrollRect = noticeRect;
            if (noticeRect.x > noticeCom.textField.width) {
                if (noticeCom && noticeCom.parent) {
                    noticeCom.parent.removeChild(noticeCom)
                }
                egret.clearInterval(noticeNnterval);
                noticeNnterval = null;
            }
        }, this, 10);
    }
    //滚动公告移动到新界面
    export function moveNoticeToPanel(group: eui.Group) {
        noticeGroup = group;
        if (noticeCom && noticeCom.parent) {
            noticeCom.parent.removeChild(noticeCom)
            noticeGroup.addChild(noticeCom);
            if (noticeRect) {
                noticeRect.width = noticeGroup.width - 50
            }
            noticeCom.noticeBg.width = noticeGroup.width
        }

    }
}